package com.haiyi.software.entity;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.jivesoftware.smack.packet.DefaultPacketExtension;
import org.jivesoftware.smack.packet.Message;
import org.xmlpull.v1.XmlPullParser;

import android.R.integer;
import android.util.Xml;

import com.haiyi.software.app.MainApplication;

public class EntityMsgVoice extends EntityMsg {

	public String getLocal_voice_path() {
		return local_voice_path;
	}

	public void setLocal_voice_path(String local_voice_path) {
		this.local_voice_path = local_voice_path;
	}

	public String getVoice() {
		return voice;
	}

	public void setVoice(String voice) {
		this.voice = voice;
	}

	protected String local_voice_path;

	protected String voice;

	protected int voice_length;

	@Override
	public Message getPacket() {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		String strServerIP = MainApplication.getInstance().getServerIP();

		String XMPPID = this.yUID + "@" + strServerIP + "/Android";

		Message msg = new Message(XMPPID, Message.Type.chat);

		DefaultPacketExtension DeExtension = new DefaultPacketExtension(
				"MyExtension", "jabber:client");

		DeExtension.setValue("name", myInfo.getUname());

		DeExtension.setValue("username", myInfo.getUsername());

		DeExtension.setValue("uhead", myInfo.getHeadSmall());

		DeExtension.setValue("voice_length", String.valueOf(this.voice_length));

		DeExtension.setValue("voice", this.voice);// voice 这保存
													// 要下载的文件id
		DeExtension.setValue("local_voice_path", this.local_voice_path);// 语音文件名。如果是你自己发的，就保存你的本地目录就可以。如果不接收的，里面存是的下载的id

		msg.setBody(body);

		msg.addExtension(DeExtension);

		return msg;
	}

	public int getVoice_length() {
		return voice_length;
	}

	public void setVoice_length(int voice_length) {
		this.voice_length = voice_length;
	}

	@Override
	public int getMessageCategory() {
		// TODO 自动生成的方法存根
		return MESSAGECATEGORY_VOICE;
	}

	@Override
	public void setAllMessage(String allMessage) {

		this.allMessage = allMessage;
		InputStream xmlInStream = new ByteArrayInputStream(
				allMessage.getBytes());

		XmlPullParser pull = Xml.newPullParser();

		try {
			pull.setInput(xmlInStream, "utf-8");

			int type = pull.getEventType();

			while (type != XmlPullParser.END_DOCUMENT) {

				switch (type) {
				case XmlPullParser.START_DOCUMENT:
					// entMsg.setAllMessage(str_xml);
					break;
				case XmlPullParser.START_TAG:

					if ("message".equals(pull.getName())) {

						this.setMessageID(pull.getAttributeValue(null, "id"));

					} else if ("body".equals(pull.getName())) {

						this.setBody(pull.nextText());

					} else if ("uhead".equals(pull.getName())) {

						this.setUhead(pull.nextText());

					} else if ("name".equals(pull.getName())) {

						this.setName(pull.nextText());

					} else if ("voice".equals(pull.getName())) {

						this.setVoice(pull.nextText());

					} else if ("local_voice_path".equals(pull.getName())) {

						this.setLocal_voice_path(pull.nextText());

					} else if ("username".equals(pull.getName())) {

						this.setUsername(pull.nextText());

					} else if ("voice_length".equals(pull.getName())) {

						this.setVoice_length(Integer.valueOf(pull.nextText()));

					}
					break;
				case XmlPullParser.END_DOCUMENT:

					break;
				}
				type = pull.next();

			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}
